package com.xiaoma.future.completableFuture_start;

import com.xiaoma.future.SmallTool;

import java.util.concurrent.CompletableFuture;

public class ThenCombine {

    public static void main(String[] args) {
        SmallTool.printTimeAndThread("小白进入餐厅");
        SmallTool.printTimeAndThread("小白点了 番茄炒蛋 + 一碗米饭");

        long currentTimeMillis = System.currentTimeMillis();
        CompletableFuture<String> cf1 = CompletableFuture.supplyAsync(() -> {
            SmallTool.printTimeAndThread("厨师炒菜");
            SmallTool.sleepMillis(3000);
            return "番茄炒蛋";
            //thenCombine 将两个stage组合，两个任务是并行的，有返回值
        }).thenCombine(CompletableFuture.supplyAsync(() -> {
            SmallTool.printTimeAndThread("服务员蒸饭");
            SmallTool.sleepMillis(3000);
            return "米饭";
        }), (dish, rice) -> {
            SmallTool.printTimeAndThread("服务员打饭");
            return String.format("%s + %s 好了", dish, rice);
        });

        SmallTool.printTimeAndThread("小白在打王者");
        SmallTool.printTimeAndThread(String.format("%s ,小白开吃", cf1.join()));

        //从耗时也能看出thenCombine的两个任务是并行
        System.out.println("耗时(s)："+(System.currentTimeMillis()-currentTimeMillis)/1000);

    }
}
